package com.engc.common.controller;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.engc.common.bean.SysUserInfo;
import com.engc.common.service.impl.SystemService;
import com.engc.common.utils.BaseUtils;
import com.engc.common.utils.ReadProperties;
import com.engc.common.utils.ToolSpring;
import com.engc.sso.Wrapper.UserWrapper;
import com.engc.sso.client.util.PathMatcher;
import com.engc.sso.client.util.ssoClientUtil;

/**
 * 校验登录拦截器
 * 
 * @author guzhixiong
 * 
 */
public class LoginInterceptor implements Filter {
	private SystemService systemService;
	private String passURL = new ReadProperties("config/Global.properties").getValue("passURL");
	private PathMatcher pathMatcher = new PathMatcher();

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse rsp, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) rsp;
		if (this.passURL(request)) {
			chain.doFilter(req, rsp);
		} else {
			HttpSession session = request.getSession(true);
			if (systemService == null) {
				systemService = (SystemService) ToolSpring.getBean("systemService");
			}
			UserWrapper ssouser = ssoClientUtil.getUserInfo(request);
			if (session.getAttribute("SysUserInfo") == null) {
				System.out.println("登录开始");
				if (ssouser == null) {
					ssoClientUtil.goToValidateCenter(request, response, request.getRequestURL().toString());
				} else {
					SysUserInfo userinfo = null;
					session.setAttribute("SysUserInfo", userinfo);
					chain.doFilter(req, rsp);
				}
			} else {
				SysUserInfo userinfo = BaseUtils.getCurrentUser(request);
				if (userinfo == null) {
					if (ssouser == null) {
						ssoClientUtil.goToValidateCenter(request, response, request.getRequestURL().toString());
					} else {
						userinfo = null;
						session.setAttribute("SysUserInfo", userinfo);
						chain.doFilter(req, rsp);
					}
				} else {
					chain.doFilter(req, rsp);
				}
			}

		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("LoginInterceptor Init.......");
	}

	/**
	 * 验证是否是不需要验证的URL
	 * 
	 * @param request
	 * @return
	 */
	public boolean passURL(HttpServletRequest request) {
		String rurl = request.getRequestURI();
		String passURLPattern = this.passURL;
		boolean isPass = false;
		if (!BaseUtils.isEmpty(passURLPattern) && !isPass) {
			String[] passURLs = passURLPattern.split(";");
			for (int i = 0; i < passURLs.length; i++) {
				isPass = pathMatcher.match(passURLs[i], rurl);
				if (isPass) {
					break;
				}
			}
		}
		return isPass;
	}
}
